﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using System.Collections.Generic;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;
using NHibernate.Criterion;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class OptVote : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            string opt = Request["opt"];
            int uid;

            if (!int.TryParse(Request["uid"], out uid))
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "参数错误" });
                return;
            }

            if (!string.IsNullOrEmpty(opt))
            {
                if (opt == "open")
                {
                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        ITransaction tran = session.BeginTransaction();

                        try
                        {
                            WxVoteConfig voteDb = session.CreateCriteria<WxVoteConfig>().Add(Expression.Eq("Id", uid)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxVoteConfig>();

                            if (voteDb.Status != "SETTING" && voteDb.Status != "PAUSE")
                            {
                                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "投票状态错误" });
                                return;
                            }

                            voteDb.Status = "RUNNING";

                            session.Update(voteDb);
                           
                            tran.Commit();
                        }
                        catch (System.Exception ex)
                        {
                            tran.Rollback();

                            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                            return;
                        }
                    }
                }
                else if (opt == "pause")
                {
                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        WxVoteConfig voteDb = session.CreateCriteria<WxVoteConfig>().Add(Expression.Eq("Id", uid)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxVoteConfig>();

                        if (voteDb.Status != "RUNNING")
                        {
                            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "投票状态错误" });
                            return;
                        }

                        voteDb.Status = "PAUSE";

                        session.Update(voteDb);
                        session.Flush();
                    }
                }
                else if (opt == "close")
                {
                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        WxVoteConfig voteDb = session.CreateCriteria<WxVoteConfig>().Add(Expression.Eq("Id", uid)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxVoteConfig>();

                        if (voteDb.Status != "RUNNING" && voteDb.Status != "PAUSE")
                        {
                            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "投票状态错误" });
                            return;
                        }

                        voteDb.Status = "END";
                        session.Flush();

                        session.Update(voteDb);
                    }
                }
                else
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "参数错误" });
                    return;
                }                
            }
            else
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "参数错误" });
                return;
            }

            _framePage.Alert(new ReturnCode() { statusCode = 200, message = "操作成功!", navTabId = _framePage.NavTabId });
        }
    }
}